<%@ Page Language="C#" MasterPageFile="~/Site.master" Title="Insert Rows with the InsertCommandEvent" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
    <h3>Supplier Information</h3>
    <AjaxData:GridView ID="GridView1" runat="server" CssClass="DataWebControlStyle" CellSpacing="0" CellPadding="3"
     AutoGenerateInsertButton="true" InsertCommandEvent="onInsert" >
       <AlternatingRowStyle CssClass="AlternatingRowStyle" />
       <RowStyle CssClass="RowStyle" />
       <HeaderStyle CssClass="HeaderStyle" />
       <EmptyDataTemplate>
        There are no records available.
       </EmptyDataTemplate>
       <EmptyDataRowStyle HorizontalAlign="Center" />
    </AjaxData:GridView>

    <script type="text/javascript">

        var _gridview;
        
        function pageLoad(sender, e)
        {
            _gridview = GridView1;
            DataService.GetAllSupplier(onLoadSuccess);
        }
        
        function onLoadSuccess(result)
        {
            _gridview.set_dataSource(result);
            _gridview.dataBind();
        }
        
        function onInsert(sender, e)
        {
             var row = e.get_row();

             var supplier = new Supplier();

             supplier.Company = row.getCellByColumnDataField('Company').value;
             supplier.ContactName = row.getCellByColumnDataField('ContactName').value;
             supplier.ContactTitle = row.getCellByColumnDataField('ContactTitle').value;
             supplier.Address = row.getCellByColumnDataField('Address').value;
             supplier.City = row.getCellByColumnDataField('City').value;
             supplier.Region = row.getCellByColumnDataField('Region').value;
             supplier.PostalCode = row.getCellByColumnDataField('PostalCode').value;
             supplier.Country = row.getCellByColumnDataField('Country').value;
             supplier.Phone = row.getCellByColumnDataField('Phone').value;
             supplier.Fax = row.getCellByColumnDataField('Fax').value;

             DataService.AddNewSupplier(supplier,
                                        function(result)
                                        {
                                            if (result == true)
                                            {
                                               alert("Row was sent to the webservice.");
                                               DataService.GetAllSupplier(onLoadSuccess);
                                            }
                                            else
                                            {
                                                alert("Insert failed");
                                            }
                                        }
                                      );
                                      
            // Instead of sending the data to the server, you could also add it 
            // directly to the Gridview using the insertRows method. 
            // See demo: /gridview/InsertRow-ClientSide.aspx
        }
        


    </script>

</asp:Content>